package com.nice.price;

import com.alibaba.fastjson.JSONObject;
import com.nice.BeanUtil;
import com.nice.Config;
import com.nice.HttpClientUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SpotCNYPrice {
    private final static Logger logger = LoggerFactory.getLogger(SpotCNYPrice.class);

    private Config config;
    //@Autowired 注入不进来。
    public SpotCNYPrice() {
        config= (Config) BeanUtil.getBean("config");
    }

    public List getSpotCNYPrice(String currency) {
        List list = new ArrayList();
        String result = "";
        JSONObject jsonObject;
        try {
            logger.info("开始查询贵金属价格");
            jsonObject = HttpClientUtil.doGet(
                    "http://api.k780.com/?app=finance.rate_cnyquot&curno="+currency+"&appkey="+config.getAppkey()+"&sign="+config.getSign()+"&format=json");
            result = jsonObject.getString("result");

            JSONObject resultJsonObject =jsonObject.getJSONObject("result");
            JSONObject currency_List = resultJsonObject.getJSONObject(currency);
            JSONObject boc_price = currency_List.getJSONObject("BOC");


            String sell_price = boc_price.getString("se_sell");
            String buy_price = boc_price.getString("se_buy");
            String middle = boc_price.getString("middle");
            if (!"JPY".equals(currency)) {
                //除100
                BigDecimal big_sell_price = new BigDecimal(sell_price).divide(new BigDecimal(100));
                sell_price = String.valueOf(big_sell_price);

                BigDecimal big_buy_price = new BigDecimal(buy_price).divide(new BigDecimal(100));
                buy_price = String.valueOf(big_buy_price);

                BigDecimal big_middle_price = new BigDecimal(middle).divide(new BigDecimal(100));
                middle = String.valueOf(big_middle_price);
            }


            String uptime = boc_price.getString("upddate");

            String change_price = boc_price.getString("change_price");
            String change_margin = boc_price.getString("change_margin");
            change_price = "0";
            change_margin = "0%";
            Map map = new HashMap();
            map.put("sell_price", sell_price);
            map.put("buy_price", buy_price);
            map.put("name", currency+"CNY");
            map.put("uptime", uptime);
            map.put("open_price", middle);
            map.put("change_price", change_price);
            map.put("change_margin", change_margin);

            list = new ArrayList();
            list.add(map);

            logger.info("查询贵金属价格{}", map);
        } catch (Exception e) {
            logger.error("查询贵金属价格异常{}", e);
        }
        return list;
    }


    public static void main(String[] args) {
//        SpotCNYPrice spotCNYPrice = new SpotCNYPrice();
//        spotCNYPrice.getSpotCNYPrice("USD");

//        SpotCNYPrice spotCNYprice = new SpotCNYPrice();
//        List USDCNY = spotCNYprice.getSpotCNYPrice("USD");
//
//        List HKDCNY = spotCNYprice.getSpotCNYPrice("HKD");
//
//        List JPYCNY = spotCNYprice.getSpotCNYPrice("JPY");
//
//        List AUDCNY = spotCNYprice.getSpotCNYPrice("AUD");
//
//        List GBPCNY = spotCNYprice.getSpotCNYPrice("GBP");
//
//        List CHFCNY = spotCNYprice.getSpotCNYPrice("CHF");
    }
}

/*

{
        "success": "1",
        "result": {
        "USD": {
        "curno": "USD",
        "curnm": "美元",
        "BOC": {
        "bankno": "BOC",
        "banknm": "中国银行",
        "se_sell": "645.0100",
        "se_buy": "642.2900",
        "cn_sell": "645.0100",
        "cn_buy": "637.0700",
        "middle": "645.7700",
        "upddate": "2021-09-03 22:33:56"
        },
        "CCB": {
        "bankno": "CCB",
        "banknm": "建设银行",
        "se_sell": "644.8700",
        "se_buy": "642.2300",
        "cn_sell": "644.8700",
        "cn_buy": "637.7300",
        "middle": "643.5200",
        "upddate": "2021-09-03 22:27:12"
        },
        "ICBC": {
        "bankno": "ICBC",
        "banknm": "工商银行",
        "se_sell": "644.6700",
        "se_buy": "642.2200",
        "cn_sell": "644.6700",
        "cn_buy": "637.0700",
        "middle": "0.0000",
        "upddate": "2021-09-03 22:35:12"
        },
        "ABC": {
        "bankno": "ABC",
        "banknm": "农业银行",
        "se_sell": "644.5870",
        "se_buy": "642.0130",
        "cn_sell": "0.0000",
        "cn_buy": "636.8670",
        "middle": "0.0000",
        "upddate": "2021-09-03 22:36:39"
        },
        "CEB": {
        "bankno": "CEB",
        "banknm": "光大银行",
        "se_sell": "642.6551",
        "se_buy": "645.2298",
        "cn_sell": "637.2482",
        "cn_buy": "645.2298",
        "middle": "0.0000",
        "upddate": "2021-09-03 22:35:00"
        }
        }
        }
        }*/
